package com.fau.wm.action.index;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.wm.action.PublicAction;
import com.fau.wm.entity.MedicalAdmin;
import com.fau.wm.entity.MedicalAllocationDetail;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @desc 库间调拨详细Action
 * @author kangyj
 * @datetime 2014年8月21日,上午12:27:41
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class AllocationDetailAction extends PublicAction implements
		ModelDriven<MedicalAllocationDetail> {

	private static final long serialVersionUID = -6165196574849111627L;
	
	private MedicalAllocationDetail allocationDetail = new MedicalAllocationDetail();
	
	/**
	 * @desc 调拨详细页面初始化
	 * @return
	 * @author kangyj
	 * @datetime 2014年8月21日,上午12:29:14
	 */
	@Action(value = "allocation_detail_init", results = {@Result(name = SUCCESS, location = "allocation_detail_init.jsp")})
	private String init(){
		return SUCCESS;
	}
	
	/**
	 * @desc 查询调拨详细
	 * @return
	 * @author kangyj
	 * @datetime 2014年8月21日,下午4:57:07
	 */
	@Action(value = "allocation_detail_view")
	public String view(){
		HttpServletResponse response = ServletActionContext.getResponse();
		list = this.baseAllService.getInStockManagerService().findallocationDetailList(allocationDetail.getAllocationId());
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 查询指定仓库指定药品调出记录
	 * @return null
	 * @author liy
	 * @datetime 2014年12月24日,上午12:28:59
	 */
	@Action(value = "allocation_out")
	public String allocationOut(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = ActionContext.getContext().getParameters();
		if(StringUtils.isNotBlank(this.allocationDetail.getMedicineId()) 
				&& StringUtils.isNotBlank(this.allocationDetail.getWarehouseId())){
		}
		pageBean = this.baseAllService.getInStockManagerService().findAllocationOut(map,this.allocationDetail.getMedicineId(),this.allocationDetail.getWarehouseId());
		this.map.clear();
		map.put("total", this.pageBean.getTotalRow());
		map.put("data", this.pageBean.getResult());
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 查询指定仓库指定药品调出记录
	 * @return null
	 * @author liy
	 * @datetime 2014年12月24日,上午8:36:11
	 */
	@Action(value = "allocation_in")
	public String allocationIn(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = ActionContext.getContext().getParameters();
		if(StringUtils.isNotBlank(this.allocationDetail.getMedicineId()) 
				&& StringUtils.isNotBlank(this.allocationDetail.getWarehouseId())){
			
		}
		pageBean = this.baseAllService.getInStockManagerService().findAllocationIn(map,this.allocationDetail.getMedicineId(),this.allocationDetail.getWarehouseId());
		this.map.clear();
		map.put("total", this.pageBean.getTotalRow());
		map.put("data", this.pageBean.getResult());
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 添加调拨明细
	 * @return
	 * @author kangyj
	 * @datetime 2014年8月21日,下午4:31:43
	 */
	@Action(value = "allocation_detail_add", results = {@Result(name = SUCCESS, location = "allocation_detail_add.jsp")})
	public String add(){
		return SUCCESS;
	}
	
	/**
	 * @desc 保存调拨明细
	 * @return
	 * @author kangyj
	 * @datetime 2014年8月21日,下午4:31:27
	 */
	@Action(value = "allocation_detail_save")
	public String save(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		this.baseAllService.getInStockManagerService().saveAllocationDetail(allocationDetail);
		map.put("success", true);
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 修改调拨明细
	 * @return
	 * @author kangyj
	 * @datetime 2014年8月21日,下午4:31:05
	 */
	@Action(value = "allocation_detail_edit", results = {@Result(name = SUCCESS, location = "allocation_detail_edit.jsp")})
	public String edit(){
		return SUCCESS;
	}
	
	/**
	 * @desc 保存修改调拨明细
	 * @return
	 * @author kangyj
	 * @datetime 2014年8月21日,下午4:30:48
	 */
	@Action(value = "allocation_detail_save_edit")
	public String saveEdit(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		this.baseAllService.getInStockManagerService().saveEditAllocationDetail(allocationDetail);
		map.put("success", true);
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 删除调拨明细
	 * @return
	 * @author kangyj
	 * @datetime 2014年8月21日,下午4:30:32
	 */
	@Action(value = "allocation_detail_del")
	public String del(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		this.baseAllService.getInStockManagerService().delAllocationDetail(allocationDetail);
		map.put("success", true);
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 根据名称模糊查询药品
	 * @return
	 * @author kangyj
	 * @datetime 2014年8月21日,下午5:39:50
	 */
	@Action(value = "allocation_detail_find_medicine")
	public String findMedicineByName(){
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		String key = request.getParameter("key");
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		list = this.baseAllService.getMedicineService().findMedicineByName(admin, key);
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 检查调出库药品数量
	 * @return
	 * @author kangyj
	 * @datetime 2014年8月21日,下午6:07:12
	 */
	@Action(value = "allocation_detail_check_inStock")
	public String checkMedicineInStock(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		Boolean flag = this.baseAllService.getInStockManagerService().checkMedicineInStock(allocationDetail.getMedicineId(), allocationDetail.getRecallWhId(), allocationDetail.getQuantity());
		if(flag) {
			map.put("able", true);
		}else {
			map.put("able", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 检查将要添加的药品是否已存在详细表中
	 * @return
	 * @author kangyj
	 * @datetime 2014年8月21日,下午6:32:54
	 */
	@Action(value = "allocation_detail_check_medicine")
	public String checkMedicineExist(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		Boolean flag = this.baseAllService.getInStockManagerService().checkMedicineExist(allocationDetail.getMedicineId(), allocationDetail.getAllocationId());
		if(flag) {
			map.put("able", true);
		}else {
			map.put("able", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 验证调拨时，产品是否存在于调出库中
	 * @return
	 * @author kangyj
	 * @datetime 2015年3月11日,下午2:31:08
	 */
	@Action(value = "allocation_valid_pro_exist")
	public String validProExist() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		String errorInfo = this.baseAllService.getInStockManagerService().validProExist(allocationDetail);
		map.put("errorInfo", errorInfo);
		formatJSON(response, map);
		return null;
	}
	
	
	@Override
	public MedicalAllocationDetail getModel() {
		return allocationDetail;
	}

}
